//
//  UIDevice.swift
//  rdb-led-banner
//
//  Created by radiobox3 on 2025/4/17.
//

import Foundation
import UIKit

extension UIDevice{
    
    
    /// 内容宽度
    static func contentWidth() -> CGFloat{
        return screenWidth() - 32.0
    }
    ///获取屏幕宽度
    static func screenWidth() -> CGFloat {
       return min(UIScreen.main.bounds.width, UIScreen.main.bounds.height)
    }
    ///获取屏幕高度
    static func screenHeight() -> CGFloat {
        return max(UIScreen.main.bounds.width, UIScreen.main.bounds.height)
    }
    
    /// 顶部安全区高度
    static func safeAreaTop() -> CGFloat {
        let scene = UIApplication.shared.connectedScenes.first
        guard let windowScene = scene as? UIWindowScene else { return 0 }
        guard let window = windowScene.windows.first else { return 0 }
        return window.safeAreaInsets.top
    }
    
    /// 底部安全区高度
    static func safeAreaBottom() -> CGFloat {
        let scene = UIApplication.shared.connectedScenes.first
        guard let windowScene = scene as? UIWindowScene else { return 0 }
        guard let window = windowScene.windows.first else { return 0 }
        return window.safeAreaInsets.bottom
    }
    
    static func statusBarHeight() -> CGFloat {
        let scene = UIApplication.shared.connectedScenes.first
        guard let windowScene = scene as? UIWindowScene else { return 0 }
        return windowScene.statusBarManager?.statusBarFrame.size.height ?? 0
    }
    
    static func navigationBarHeight() -> CGFloat {
        return statusBarHeight() + 44
    }
    
    static func getUniqueDeviceIdentifier() -> String {
        
        let key = "rdb_uuid"
        
        if let uuid = RdbKeychain.read(key: key) {
            return uuid
        } else {
            var newUuidStr = UUID().uuidString
            if newUuidStr.isEmpty {
                newUuidStr = "\(Int64(Date().timeIntervalSince1970))"
            }
            if RdbKeychain.save(value: newUuidStr, forKey: key) {
                return newUuidStr
            }
            return newUuidStr
        }
    }
}
